package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.ProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class nursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Override
    //查询所有护理计划
    public List<NursingPlanVo> getAll() {
        List<NursingPlanVo> nursingPlanVos =nursingPlanMapper.getAll();
        return nursingPlanVos;
    }

    @Override
    @Transactional
    //添加护理计划
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        if (nursingPlanDto.getStatus()==null){
            nursingPlan.setStatus(0);
            nursingPlanMapper.insert(nursingPlan);
        }else {
            nursingPlanMapper.insert(nursingPlan);
        }
        Integer searchID = nursingPlanMapper.searchID(nursingPlan);
        List<ProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        for (ProjectPlan projectPlan : projectPlans) {
            projectPlan.setPlanId(searchID);
            nursingPlanMapper.insertDto(projectPlan);
        }
    }

    @Override
    //分页查询
    public PageResponse<NursingPlanVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlanVo> nursingPlanVos = nursingPlanMapper.getByPage(name,status);
        PageResponse<NursingPlanVo> nursingPlanVoPageResponse = PageResponse.of(nursingPlanVos, NursingPlanVo.class);
        List<NursingPlanVo> nursingPlanVosCount=nursingPlanVoPageResponse.getRecords();
        for (NursingPlanVo nursingPlanVo : nursingPlanVosCount){
            Long id = nursingPlanVo.getId();
            Integer count =nursingPlanMapper.count(id);
            nursingPlanVo.setCount(count);
        }
        return nursingPlanVoPageResponse;
    }

    @Override
    //根据id查询
    public NursingPlanVo getById(Long id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.getById(id);
        return nursingPlanVo;
    }
    //修改
    @Transactional
    @Override
    public void update(Long id, NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setId(id);
        if (nursingPlanDto.getStatus()==null){
            nursingPlan.setStatus(0);
           nursingPlanMapper.update(nursingPlan);
        }else {
            nursingPlanMapper.update(nursingPlan);
        }
        nursingPlanMapper.deleteByNPPId(id);
        List<ProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        for (ProjectPlan projectPlan : projectPlans) {
            Integer ids= id.intValue();
            projectPlan.setPlanId(ids);
            nursingPlanMapper.insertDto(projectPlan);
        }


    }
    //删除
    @Transactional
    @Override
    public void deleteById(Long id) {
        nursingPlanMapper.deleteById(id);
        nursingPlanMapper.deleteByNPPId(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id,status);
    }


}
